package org.csu.onlineedubackend.mapper;

import org.apache.ibatis.annotations.*;
import org.csu.onlineedubackend.entity.FriendRelationship;
import org.csu.onlineedubackend.entity.User;

import java.util.List;

@Mapper
public interface FriendRelationshipMapper {
    
    @Insert("INSERT INTO friend_relationships (user_id, friend_id, created_at) " +
            "VALUES (#{userId}, #{friendId}, NOW()) " +
            "ON DUPLICATE KEY UPDATE created_at = NOW()")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(FriendRelationship friendRelationship);
    
    @Select("SELECT * FROM friend_relationships WHERE id = #{id}")
    FriendRelationship findById(Long id);
    
    @Select("SELECT * FROM friend_relationships WHERE user_id = #{userId} AND friend_id = #{friendId}")
    FriendRelationship findByUserAndFriend(@Param("userId") Integer userId, @Param("friendId") Integer friendId);
    
    @Select("SELECT * FROM friend_relationships WHERE user_id = #{userId}")
    List<FriendRelationship> findByUserId(Integer userId);
    
    @Delete("DELETE FROM friend_relationships WHERE user_id = #{userId} AND friend_id = #{friendId}")
    int deleteByUserAndFriend(@Param("userId") Integer userId, @Param("friendId") Integer friendId);
    
    @Select("SELECT u.* FROM user u JOIN friend_relationships fr ON u.id = fr.friend_id WHERE fr.user_id = #{userId}")
    List<User> findFriendsByUserId(@Param("userId") int userId);
    
    @Select("SELECT COUNT(*) > 0 FROM friend_relationships " +
            "WHERE (user_id = #{userId1} AND friend_id = #{userId2}) " +
            "OR (user_id = #{userId2} AND friend_id = #{userId1})")
    boolean areFriends(@Param("userId1") Integer userId1, @Param("userId2") Integer userId2);
} 